// JavaScript Document
$(document).ready(function(){
						   $('#btnAddVote').click(function(){
														   $('#updateVote h3').html('Tạo bình chọn mới');														   
														  ResetModalRole();
														  $('#task').val('add');
														  $('#lblEnd').hide();
														  
														   });
						    $('#tbResult a.update').live('click',function(){
													$('#updateVote h3').html('Cập nhật bình chọn');
													ResetModalRole();
													var id=$(this).parent().parent().children().eq(0).text();
													$('#task').val('update');
													$('#voteid').val(id);
													$.ajax({
														 url:"../Admin/process/Vote_process.php",
														 data:{voteid: id,task:"getbyid"},
														 type:"POST",
														 dataType:"json",
														 cache:false,
														 success:function(res){
															 $('#cbxTopicVote option[value="'+res.topic+'"]').attr('selected','selected');
															 if(res.isend==1)
															 {
															 	$('#chkIsEnd').attr('checked','checked');
															 }
															 else
															 {
																
																 $('#chkIsEnd').removeAttr('checked');
															 }
															 
															 $('#txtVoteName').val(res.name);
															 $('#txtQuestion').val(res.content);
															 $('#txtSum').text(res.total);
															// alert(res.detail.length);
											for(var j=0;j<res.detail.length;j++)
											{
												
								var html='<tr><td class="hide">'+res.detail[j].id+'</td> <td>'+res.detail[j].content+'</td><td>'+res.detail[j].agreenum+'</td><td>'+res.detail[j].percent+'</td><td><a href="#" title="Xóa" class="deldetail"><img src="../Images/Icons/delete.png" alt="delete"/></a></td></tr>';
								//alert(html);
								$('#tbDetail tr:last').after(html);
											}
															 }
														 });
																			
																		  });
							 $('#tbResult a.delete').live('click',function(){
											if(confirm('Bạn thực sự muốn xóa?'))
											{
													var id=$(this).parent().parent().children().eq(0).text();
													var row=$(this).parent().parent();
													$.ajax({
														 url:"../Admin/process/Vote_process.php",
														 data:{voteid: id,task:"delete"},
														 type:"POST",
														 dataType:"json",
														 cache:false,
														 success:function(res){
															if(res.ok)
															{
																row.remove();
															}
															alert(res.msg);
															 }
														 });
											}
																		   });
							 $('#btnAddAnswer').click(function(){
										var answer=prompt('Hãy nhập câu trả lời');
										//alert(answer);
										if(answer!='' && answer!=null)
										{
											
										$.ajax({
											    url:"../Admin/process/Vote_process.php",
												data:{task:"adddetail",content:answer},
												type:"POST",
												dataType:"json",
												cache:false,
												success:function(res){
														if(res.ok)
															{
$('#tbDetail tr:last').after('<tr><td class="hide">'+res.id+'</td> <td>'+answer+'</td><td>0</td><td>0</td><td><a href="#" title="Xóa" class="deldetail"><img src="../Images/Icons/delete.png" alt="delete" /></a></td></tr>');
															}
															
															 }
											   });

										}
															   });
							 $('#btnAddVote').click(function(){
									$.ajax({
												url:"../Admin/process/Vote_process.php",
												data:{task:"reset"},
												type:"post",
												success:function()
														{
															
														}
										 });
															
															 });
								
								
								$('#tbDetail a.deldetail').live('click',function(){
											if(confirm('Bạn thực sự muốn xóa?'))
											{
													var id=$(this).parent().parent().children().eq(0).text();
													var row=$(this).parent().parent();
													$.ajax({
														 url:"../Admin/process/Vote_process.php",
														 data:{id: id,task:"deldetail"},
														 type:"POST",
														 dataType:"json",
														 cache:false,
														 success:function(res){
															if(res.ok)
															{
																row.remove();
															}
															
															 }
														 });
											}
																				 });
											
						   });
function ResetModalRole(){
	
	$('#txtVoteName').val('');
	$('#txtQuestion').val('');
	$('#lblEnd').show();
	$('#tbDetail tr:not(:first)').remove();
	 $('#txtSum').text('0');

	}